<?php

namespace common\models\bonus;

use Yii;

/**
 * This is the model class for table "{{%bonus_day}}".
 *
 * @property int $id
 * @property int $created_at 日期
 * @property string $bonus_1 
 * @property string $bonus_2 
 * @property string $bonus_3 
 * @property string $bonus_all 总额
 */
class BonusDay extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%bonus_day}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['created_at'], 'integer'],
            [['bonus_1', 'bonus_2', 'bonus_3','bonus_4','bonus_all','bonus_5','bonus_6','bonus_7','bonus_8','bonus_9','bonus_10', 'payment'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        $arr = Bonus::bonusNameMap();
        $names = [];
        foreach ($arr as $key => $value) {
            $index = $key + 1;
            $names['bonus_'.$index] = $value;
        }
        return array_merge([
            'id' => 'ID',
            'created_at' => '日期',
            'bonus_all' => '总额',
            'payment' => '实发',
        ], $names);
    }

    public static function updateDayDate(Bonus $bonus)
    {
        $day = strtotime(date('Y-m-d', $bonus->created_at));
        $model = self::find()->where(['created_at'=>$day])->one();
        if (!$model) {
            $model = new self;
            $model->created_at = $day;
            $model->bonus_1 = 0;
            $model->bonus_2 = 0;
            $model->bonus_3 = 0;
            $model->bonus_4 = 0;
            $model->bonus_5 = 0;
            $model->bonus_6 = 0;
            $model->bonus_7 = 0;
            $model->bonus_8 = 0;
            $model->bonus_9 = 0;
            $model->bonus_10 = 0;
            $model->payment = 0;
            $model->bonus_all = 0;
            if (!$model->save()) {
                throw new \Exception('奖金日汇总失败', 1);
            }
        }
        $fileName = 'bonus_'.$bonus->bonus_type;
        // $attributes = $this->attributeLabels();
        // if (!isset($attributes[$fileName])) {
        //     throw new \Exception('奖项不明确', 1);
        // }
        $update = self::updateAllCounters([$fileName=>$bonus->amount, 'bonus_all'=>$bonus->amount, 'payment'=>$bonus->payment], ['id'=>$model->id]);
        return $update;
    }

    public static function updateDayDateDec(Bonus $bonus)
    {
        $day = strtotime(date('Y-m-d', $bonus->created_at));
        $model = self::find()->where(['created_at'=>$day])->one();
        if (!$model) {
            $model = new self;
            $model->created_at = $day;
            $model->bonus_1 = 0;
            $model->bonus_2 = 0;
            $model->bonus_3 = 0;
            $model->bonus_4 = 0;
            $model->bonus_5 = 0;
            $model->bonus_6 = 0;
            $model->bonus_7 = 0;
            $model->bonus_8 = 0;
            $model->bonus_9 = 0;
            $model->bonus_10 = 0;
            $model->payment = 0;
            $model->bonus_all = 0;
            if (!$model->save()) {
                throw new \Exception('奖金日汇总失败', 1);
            }
        }
        $fileName = 'bonus_'.$bonus->bonus_type;
        // $attributes = $this->attributeLabels();
        // if (!isset($attributes[$fileName])) {
        //     throw new \Exception('奖项不明确', 1);
        // }
        $update = self::updateAllCounters([$fileName=>-$bonus->amount, 'bonus_all'=>-$bonus->amount, 'payment'=>-$bonus->payment], ['id'=>$model->id]);
        return $update;
    }
}
